package com.chejianer.phone.utils;

/**
 * Created by Chejianer on 2016/8/31.
 */
public class CodeUtil {

        public static void main(String[] args) {
            String outString="";
            outString=fillStr("123", 9, false, "0");
            System.out.println("false"+outString);
            outString=fillStr("123", 9, true, "0");
            System.out.println("true"+outString);

        }
        /**
         *
         * @param value
         * @param count
         * @param frontORback
         * @param fillChar
         * @return if frontORback is ture ,return the (count-value.length) fillchar added to the front of the value
         * if frontORback is false,return the (count-value.length) fillchar added to the back of the value
         * for instance : value="123",count=9,frontORback=true,fillchar="0"<br>
         * return "123000000"
         *
         */
    private static String fillStr(String value, int count, boolean frontORback, String fillChar) {
        String rtvalue = value;
        if (rtvalue == null) {
            rtvalue = "";
            for (int i = 0; i < count; i++)
                if (frontORback)
                    rtvalue = String.valueOf(rtvalue)
                            + String.valueOf(fillChar);
                else
                    rtvalue = String.valueOf(fillChar)
                            + String.valueOf(rtvalue);
        } else {
            int len = rtvalue.length();
            if (len > count) {
                rtvalue = rtvalue.substring(0, count);
            } else {
                int a = count - len;
                for (int i = 0; i < a; i++)
                    if (frontORback)
                        rtvalue = String.valueOf(rtvalue)
                                + String.valueOf(fillChar);
                    else
                        rtvalue = String.valueOf(fillChar)
                                + String.valueOf(rtvalue);
            }
        }
        return rtvalue;
    }
}
